package cn.iocoder.yudao.module.hs.controller.admin.materialmode;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.materialmode.vo.MaterialModePageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.materialmode.vo.MaterialModeRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.materialmode.vo.MaterialModeSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.materialmode.MaterialModeDO;
import cn.iocoder.yudao.module.hs.service.materialmode.MaterialModeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 机型信息")
@RestController
@RequestMapping("/hs/material-mode")
@Validated
public class MaterialModeController {

    @Resource
    private MaterialModeService materialModeService;

    @PostMapping("/create")
    @Operation(summary = "创建机型信息")
    @PreAuthorize("@ss.hasPermission('hs:material-mode:create')")
    public CommonResult<Boolean> createMaterialMode(@Valid @RequestBody MaterialModeSaveReqVO createReqVO) {
        materialModeService.createMaterialMode(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新机型信息")
    @PreAuthorize("@ss.hasPermission('hs:material-mode:update')")
    public CommonResult<Boolean> updateMaterialMode(@Valid @RequestBody MaterialModeSaveReqVO updateReqVO) {
        materialModeService.updateMaterialMode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机型信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:material-mode:delete')")
    public CommonResult<Boolean> deleteMaterialMode(@RequestParam("id") Long id) {
        materialModeService.deleteMaterialMode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机型信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:material-mode:query')")
    public CommonResult<MaterialModeRespVO> getMaterialMode(@RequestParam("id") Long id) {
        MaterialModeDO materialMode = materialModeService.getMaterialMode(id);
        MaterialModeRespVO respVO = BeanUtils.toBean(materialMode, MaterialModeRespVO.class);
        respVO.setTripartiteWarehouseIds(Collections.singletonList(materialMode.getTripartiteWarehouseId()));
        return success(respVO);
    }

    @GetMapping("/get/{materialCode}")
    @Operation(summary = "根据materialCode获得机型信息")
    @PermitAll
    public CommonResult<MaterialModeRespVO> getMaterialModeByMaterialCode(@PathVariable String materialCode) {
        MaterialModeDO materialModeDO = materialModeService.getMaterialModeByMaterialCode(materialCode);
        return success(BeanUtils.toBean(materialModeDO, MaterialModeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机型信息分页")
    @PreAuthorize("@ss.hasPermission('hs:material-mode:query')")
    public CommonResult<PageResult<MaterialModeRespVO>> getMaterialModePage(@Valid MaterialModePageReqVO pageReqVO) {
        PageResult<MaterialModeDO> pageResult = materialModeService.getMaterialModePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialModeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机型信息 Excel")
    @PreAuthorize("@ss.hasPermission('hs:material-mode:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialModeExcel(@Valid MaterialModePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialModeDO> list = materialModeService.getMaterialModePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机型信息.xls", "数据", MaterialModeRespVO.class,
                        BeanUtils.toBean(list, MaterialModeRespVO.class));
    }

}